var e = require("../../../@babel/runtime/helpers/interopRequireDefault").default, t = require("../../../@babel/runtime/helpers/defineProperty"), a = e(require("@vant/weapp/dialog/dialog.js")), o = e(require("../../../api/api_queryRemain.js")), n = e(require("../../../utils/Tips")), i = e(require("../../../behaviors/customNav.js")), r = e(require("../../../behaviors/auth.js")), s = e(require("../../../behaviors/openid.js")), l = e(require("../../../kta/index")), c = require("../../../utils/util"), u = require("../../../utils/number"), d = e(require("./businessData")), h = e(require("../../../libs/wxsubscribe/index")), m = require("../../../config/Links"), f = getApp(), g = f.eventBus;

Component({
    behaviors: [ i.default, r.default, s.default ],
    properties: {},
    data: {
        pageShow: !1,
        userStatus: "",
        banners: "",
        bgSrc: "",
        cardTag: "",
        phone: "",
        balance: "",
        flowProgress: 0,
        ryzbFlowValue: "",
        directFlowValue: "",
        commonFlowValue: "",
        ryzbType: "",
        liveRoomList: [],
        businessData: d.default,
        nativeComponentShow: !0,
        circleGradient: {
            "0%": "#987EFF",
            "100%": "#3572FD"
        },
        circleSize: 130,
        showProtocol: !1,
        imageBaseUrl: m.imageBaseUrl.queryRemain,
        curNum: 0
    },
    methods: {
        init: function() {
            var e = this;
            this.setData({
                curNum: (0, c.genCurNum)()
            }), this.computeCircleSize(), n.default.loading(), Promise.all([ this.getUserInfo(), this.getLiveRoomList() ]).then(function() {
                n.default.loaded(), e.setData({
                    pageShow: !0
                });
            }).catch(function(e) {
                console.log(e), n.default.loaded();
            });
        },
        handleContact: function(e) {
            console.log(e);
        },
        toCustomerPage: function() {
            wx.navigateTo({
                url: "/pages/webview/index?url=".concat(encodeURIComponent("https://m.10010.com/myorder/?isFbJD=1"))
            });
        },
        getUserInfo: function() {
            var e = this;
            return this.getAuthUserInfo({
                forceUpdate: !0
            }).then(function(t) {
                e.handleUserInfoRes(t);
            });
        },
        handleUserInfoRes: function(e) {
            if (e) {
                var t = (0, c.isUnd)(e.data) ? {} : e.data, a = t.cardTag, o = t.phone, n = (e.data ? e.data.balance : {}) || {}, i = n.rzbFlowValue, r = n.yzbFlowValue, s = n.balance, l = n.commonAllValue, u = n.realFee, d = n.voiceValue, h = n.directFlowValue, m = n.commonFlowValue, f = {
                    ryzbUnit: "日",
                    balance: "- -",
                    realFee: "- -",
                    voiceValue: "- -",
                    directFlowValue: "- -",
                    commonFlowValue: "- -",
                    flowProgress: 0,
                    ryzbFlowValue: "- -",
                    cardTag: a,
                    phone: o,
                    banners: e.data.banners
                };
                if (o) {
                    if (0 === a) {
                        var g = Object.assign(f, {
                            userStatus: "NOTWK"
                        });
                        this.setData(g);
                    } else if (1 === a) {
                        var p = Object.assign(f, {
                            userStatus: "WK",
                            ryzbFlowValue: this.transformData(i),
                            balance: this.transformData(s),
                            realFee: this.transformData(u),
                            voiceValue: this.transformData(d),
                            commonFlowValue: this.transformData(m),
                            flowProgress: this.computeFlowProgress(m, l)
                        });
                        this.setData(p);
                    } else if (2 === a) {
                        var w = Object.assign(f, {
                            userStatus: "WK",
                            ryzbUnit: "月",
                            ryzbFlowValue: this.transformData(r),
                            balance: this.transformData(s),
                            realFee: this.transformData(u),
                            voiceValue: this.transformData(d),
                            commonFlowValue: this.transformData(m),
                            flowProgress: this.computeFlowProgress(m, l)
                        });
                        this.setData(w);
                    } else if (3 === a) {
                        var v = i ? "日" : "月", b = Object.assign(f, {
                            userStatus: "WK",
                            ryzbUnit: v,
                            ryzbFlowValue: this.transformData("日" === v ? i : r),
                            balance: this.transformData(s),
                            realFee: this.transformData(u),
                            voiceValue: this.transformData(d),
                            directFlowValue: this.transformData(h),
                            commonFlowValue: this.transformData(m),
                            flowProgress: this.computeFlowProgress(m, l)
                        });
                        this.setData(b);
                    }
                } else {
                    var x = Object.assign(f, {
                        userStatus: "NOTBIND"
                    });
                    this.setData(x);
                }
            }
        },
        getLiveRoomList: function() {
            var e = this;
            return o.default.getLiveRoomList().then(function(t) {
                if ("0" !== t.code) e.dialog().alert({
                    context: e,
                    message: t.message
                }); else if (t.data && t.data.length > 0) {
                    var a = t.data.filter(function(e) {
                        return e.endTime >= Date.now();
                    }).map(function(t) {
                        var a = t.startTime, o = t.endTime, n = (0, c.dateFormat)(new Date(a), "MM-DD HH:mm"), i = "直播中", r = "ing";
                        return Date.now() < a && (i = "预告", r = "advance", e.updateLiveRoomStatus(t.roomId)), 
                        {
                            startTime: a,
                            startTimeFormat: n,
                            statusText: i,
                            liveStatus: r,
                            roomId: t.roomId,
                            coverImg: t.coverImg,
                            name: t.name,
                            endTime: o,
                            detail: t.detail,
                            appId: t.appId || null,
                            path: t.path || null
                        };
                    });
                    e.setData({
                        liveRoomList: a
                    });
                }
            }).catch(function(e) {
                console.log(e);
            });
        },
        updateLiveRoomStatus: function(e) {
            var a = this;
            this["interval" + e] = setInterval(function() {
                var o, n = a.data.liveRoomList, i = n.findIndex(function(t) {
                    return t.roomId === e;
                }), r = Date.now() > n[i].startTime;
                i && r && (a.setData((t(o = {}, "liveRoomList[".concat(i, "].statusText"), "直播中"), 
                t(o, "liveRoomList[".concat(i, "].liveStatus"), "ing"), o)), clearInterval(a["interval" + e]));
            }, 6e4);
        },
        handleLiveRoomClick: function(e) {
            var t = e.currentTarget.dataset, a = t.room, o = t.index, n = this.data.liveRoomList[o];
            n.path ? n.appId ? wx.navigateToMiniProgram({
                appId: n.appId,
                path: n.path
            }) : wx.navigateTo({
                url: n.path
            }) : this.getOpenid(a).then(function(e) {
                f.kta.clickStat("ace403c5f52f4b75b599e5420adaeb94", {
                    roomId: a,
                    openid: e
                }), wx.navigateTo({
                    url: "plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=".concat(a)
                });
            }).catch(function() {
                f.kta.clickStat("ace403c5f52f4b75b599e5420adaeb94", {
                    roomId: a
                }), wx.navigateTo({
                    url: "plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=".concat(a)
                });
            });
        },
        handleBusinessClick: function(e) {
            var t = e.currentTarget.dataset.index, a = d.default[t].id;
            "1" === a ? wx.navigateTo({
                url: "/pages/myPackage/index"
            }) : "2" === a ? this.handleChargeFee() : "3" === a ? this.handleUnlockFlow() : "4" === a && wx.navigateTo({
                url: "/pages/buyRecord/index"
            });
        },
        handleApplyBtnClick: function(e) {
            var t = e.currentTarget.dataset.img;
            if (l.default.clickStat("965d0f56f0cf46a09646a24ece1c56f3"), "4" === t) wx.navigateToMiniProgram({
                appId: "wx51155c5475dc59b4",
                path: "packageActivity/pages/unicom-appoint/unicom-appoint"
            }); else {
                var a = [ null, "https://txwk.10010.com/kcardorder/gotoUid?state=KCARD_WKPEN_COMMOM_ORDERLAND&pageKey=04360f19B9&channel=01-2611-2702-9999&ADTAG=01-2611-2702-9999", "https://txwk.10010.com/t/jwy3o", "https://txwk.10010.com/t/6n9u9" ][t];
                wx.navigateTo({
                    url: "/pages/webview/index?url=".concat(encodeURIComponent(a))
                });
            }
        },
        handleChargeFee: function() {
            wx.navigateTo({
                url: "/pages/charge/index"
            }), g.emit("toChargePage", {
                phone: this.data.phone
            });
        },
        handleUnlockFlow: function() {
            var e = this.data.userStatus;
            "NOTBIND" === e || "NOTWK" === e ? this.dialog().confirm({
                context: this,
                message: "您需要绑定王卡号码，才可办理相关业务。",
                confirmButtonText: "确定",
                showCancelButton: !1
            }) : wx.navigateTo({
                url: "/pages/unlockFlow/index"
            });
        },
        transformData: function(e) {
            return "NA" === (e = e ? e.toString() : "") ? "暂无数据" : "NB" === e ? "系统升级中，暂无数据" : (0, 
            c.testNumber)(e) ? e : "- -";
        },
        computeFlowProgress: function(e, t) {
            var a;
            return e = Number(e), t = Number(t), 0 === e && (a = 0), (0, c.isNumber)(e) && (0, 
            c.isNumber)(t) && (a = Math.floor(100 * (0, u.subNum)(1, (0, u.divNum)(e, t)))), 
            a >= 99 && (a = 98), a <= 1 && a > 0 && (a = 2), a;
        },
        afterBindgetphonenumber: function() {
            var e = this;
            this.closeProtocolModal(), this.getUserInfo().then(function(t) {
                e.handleUserInfoRes(t);
            });
        },
        afterBindgetphonenumberErr: function() {
            this.closeProtocolModal();
        },
        afterBinduserinfo: function() {
            var e = this;
            this.getUserInfo().then(function(t) {
                e.handleUserInfoRes(t);
            });
        },
        handleBannerClick: function(e) {
            var t = e.currentTarget.dataset.index, a = this.data.banners[t];
            a.linkUrl ? (g.emit("toWebviewPage", {
                url: a.linkUrl
            }), wx.navigateTo({
                url: "/pages/webview/index"
            })) : a.appId && (a.appId === f.globalData.appid ? wx.navigateTo({
                url: "/".concat(a.path)
            }) : wx.navigateToMiniProgram({
                appId: a.appId,
                path: a.path
            }));
        },
        handleNotWkSwitch: function() {
            this.dialog().alert({
                context: this,
                confirmButtonOpenType: "getPhoneNumber",
                confirmButtonText: "继续",
                richTextMessage: !0,
                richText: [ {
                    name: "div",
                    children: [ {
                        type: "text",
                        text: "如果您微信绑定的号码不是腾讯王卡号码，您可点击"
                    }, {
                        name: "strong",
                        children: [ {
                            type: "text",
                            text: "“使用其他手机号码”"
                        } ]
                    }, {
                        type: "text",
                        text: "添加腾讯王卡号码进行绑定。"
                    } ]
                } ]
            });
        },
        dialog: function() {
            return this.setData({
                nativeComponentShow: !1
            }), a.default;
        },
        onDialogClose: function() {
            this.setData({
                nativeComponentShow: !0
            });
        },
        computeCircleSize: function() {
            this.windowWidth = this.windowWidth || wx.getSystemInfoSync().windowWidth, this.setData({
                circleSize: parseInt(130 / 375 * this.windowWidth, 10)
            });
        },
        handlesSubscribeFive: function(e) {
            var t = this, a = e.currentTarget.dataset.scene, o = getApp().globalData.token;
            new h.default({
                env: "miniprogram",
                cookie: o,
                templateId: "JAn5Pg4wgg_F82F6bC3V1DN2WR2AeVjF_XXR_kQwd7g",
                seq: Date.now(),
                action_id: "new-activity-subscribe",
                scene: a,
                extra_data: "",
                success: function(e) {
                    console.log(e), t.closeFiveModal(), wx.showToast({
                        title: "订阅成功",
                        icon: "none",
                        duration: 2e3
                    });
                },
                error: function(e) {
                    console.log(e), t.closeFiveModal(), wx.showToast({
                        title: e.msg || e.message || "操作失败，请稍后再试!",
                        icon: "none",
                        duration: 2e3
                    });
                }
            });
        },
        toProtocolPage: function() {
            wx.navigateTo({
                url: "/pages/webview/index?url=".concat(encodeURIComponent("https://txwk.10010.com/wkpen/view/0b27c3661b.html"))
            });
        },
        showProtocolModal: function() {
            this.setData({
                showProtocol: !0
            });
        },
        closeProtocolModal: function() {
            this.setData({
                showProtocol: !1
            });
        },
        handleClickNoWKPanel: function() {
            wx.navigateTo({
                url: "/pages/webview/index?url=".concat(encodeURIComponent("https://txwk.10010.com/kcardorder/gotoUid?state=KCARD_WKPEN_COMMOM_ORDERLAND&pageKey=04360f19B9&channel=01-2611-agah-9999&ADTAG=01-2611-agah-9999"))
            });
        }
    },
    created: function() {
        var e = this;
        this.refreshSub = g.on("queryRemainIndexRefresh", function() {
            e.init();
        });
    },
    attached: function() {
        this.setData({
            navTop: this.getNavTop(23) + "px",
            navOffset: this.getNavTop(23) + 23 + 28 + "px"
        }), this.init(), console.log(this.data.businessData);
    },
    ready: function() {},
    moved: function() {},
    detached: function() {
        this.refreshSub && this.refreshSub.off();
    }
});